<?php

namespace App\resources\Api;

use App\Enum\OrderEnum;
use App\Enum\TravelEnum;
use App\Models\ConfigModel;
use Illuminate\Http\Resources\Json\JsonResource;

class GetOrderListCollection extends JsonResource
{
    private $_vehicleType = null;
    private $_vehicleUseType = null;
    public function __construct($resource)
    {
        parent::__construct($resource);
        // 获取车型和用车类型 20230810 by LC
        if( empty($this->_vehicleType)){$this->_vehicleType = json_decode(ConfigModel::getVehicleType(), true);}
        if( empty($this->_vehicleUseType)){$this->_vehicleUseType = json_decode(ConfigModel::getVehicleUseType(), true);}

    }
    public function toArray($request)
    {
        $item = $this->resource;
        $startLocation = $item['start_location'] ? explode(',', $item['start_location']) : [];
        $endLocation = $item['end_location'] ? explode(',', $item['end_location']) : [];
        return [
            'id' => $item['id'],
            'order_no' => $item['order_no'] ?? 0,
            'user_id' => $item['user_id'] ?? 0,
            'driver_id' => $item['driver_id'] ?? 0,
            'start_location' => $item['start_location'] ?? '',
            'start_location_x' => !empty($startLocation) ? $startLocation[0] : 0,
            'start_location_y' => !empty($startLocation) ? $startLocation[1] : 0,
            'end_location' => $item['end_location'] ?? '',
            'is_return' =>$item['is_return']?? 0,
            'end_location_x' => !empty($endLocation) ? $endLocation[0] : 0,
            'end_location_y' => !empty($endLocation) ? $endLocation[1] : 0,
            'start_time' => $item['start_time'] ? date('Y/m/d H:i:s', $item['start_time']) : 0,
            'end_time' => $item['end_time'] ? date('Y/m/d H:i:s', $item['end_time']) : 0,
            'start_address' => $item['start_address'] ?? '',
            'start_address_detail' => $item['start_address_detail'] ?? '',
            'end_address' => $item['end_address'] ?? '',
            'end_address_detail' => $item['end_address_detail'] ?? '',
            'distance' => $item['distance'] ? ($item['distance'] / 1000) : 0,
            'amount' => $item['amount'] ? number_format($item['amount'], 2) : '',
            'platform_id' => $item['platform_id'] ?? '',
            'platform_type' => isset($item['platform_id'] )? TravelEnum::APP_PLATFORM_NAMES[$item['platform_id']] : '',
            'order_type' => $item['order_type'] ?? 1,
            'is_accompany' => $item['is_accompany'] ?? 0,
            'payment_method' => $item['payment_method'] ?? 0,
            'payment_method_name' => $item['payment_method'] == 1 ? '数字人民币' : '积分',
            'status' => $item['status'],
            'status_name' => OrderEnum::STATUS_NAME[$item['status']],
            'vehicle_type_name' => $this->_vehicleType[$item['vehicle_type']] ?? '-',
            'vehicle_use_type_name' => $this->_vehicleUseType[$item['vehicle_use_type']] ?? '-',
        ];
    }
}
